const Sequelize = require('sequelize')
module.exports = (sequelize, DataTypes) => {
  return AclPermission.init(sequelize, DataTypes)
}

class AclPermission extends Sequelize.Model {
  static init(sequelize, DataTypes) {
    super.init(
      {
        id: {
          autoIncrement: true,
          type: DataTypes.INTEGER,
          allowNull: false,
          primaryKey: true,
        },
        parentId: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: -1,
          comment: '父级权限id',
          field: 'parent_id',
        },
        name: {
          type: DataTypes.STRING(50),
          allowNull: false,
          comment: '权限名称',
        },
        description: {
          type: DataTypes.STRING(50),
          allowNull: false,
          defaultValue: '',
          comment: '权限描述',
        },
        url: {
          type: DataTypes.STRING(50),
          allowNull: false,
          defaultValue: '',
          comment: '后端请求验证',
        },
        perms: {
          type: DataTypes.STRING(50),
          allowNull: false,
          defaultValue: '',
          comment: '权限标识',
        },
        type: {
          type: DataTypes.TINYINT,
          allowNull: false,
          defaultValue: 0,
          comment: '类型 0：目录,1：菜单,2：组件',
        },
        component: {
          type: DataTypes.STRING(32),
          allowNull: false,
          defaultValue: '',
          comment: '前端组件(前后端分离)',
        },
        componentName: {
          type: DataTypes.STRING(32),
          allowNull: true,
          comment: '组件名字',
          field: 'component_name',
        },
        redirect: {
          type: DataTypes.STRING(64),
          allowNull: true,
          comment: '前端重定向地址',
        },
        route: {
          type: DataTypes.STRING(64),
          allowNull: true,
          comment: '前端路由地址',
        },
        isRoute: {
          type: DataTypes.TINYINT,
          allowNull: true,
          defaultValue: 1,
          comment: '是否路由菜单: 0:不是  1:是（默认值1）',
          field: 'is_route',
        },
        keepAlive: {
          type: DataTypes.TINYINT,
          allowNull: true,
          defaultValue: 1,
          comment: '是否缓存该页面:    1:是   0:不是',
          field: 'keep_alive',
        },
        openTarget: {
          type: DataTypes.TINYINT,
          allowNull: true,
          defaultValue: 0,
          comment: '外链菜单打开方式 0/内部打开 1/外部打开',
          field: 'open_target',
        },
        hidden: {
          type: DataTypes.TINYINT,
          allowNull: true,
          defaultValue: 0,
          comment: '是否隐藏路由: 0否,1是',
        },
        orderNum: {
          type: DataTypes.TINYINT,
          allowNull: false,
          defaultValue: 0,
          comment: '排序',
          field: 'order_num',
        },
        icon: {
          type: DataTypes.STRING(50),
          allowNull: true,
          defaultValue: '',
          comment: '图标',
        },
        status: {
          type: DataTypes.TINYINT,
          allowNull: false,
          defaultValue: 1,
          comment: '状态：1有效；0禁用',
        },
      },
      {
        sequelize,
        tableName: 'acl_permission',
        timestamps: true,
        underscored: true,
        paranoid: true,
        createdAt: 'createTime',
        updatedAt: 'updateTime',
        deletedAt: 'deleteTime',
        indexes: [
          {
            name: 'PRIMARY',
            unique: true,
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
          {
            name: 'perms',
            using: 'BTREE',
            fields: [{ name: 'perms' }],
          },
        ],
      },
    )
    return AclPermission
  }
}
